﻿using Customer.API.Applcation.Command.FinanceCommand;
using Customer.Infrstructre.FinanceManager;
using MediatR;

namespace Customer.API.Applcation.CommandHandlers.FinanceCommandHandler
{
    /// <summary>
    /// 删除应收管理中介者
    /// </summary>
    public class DelReceivableCommandHandler:IRequestHandler<DelReceivableCommand,int>
    {
        private readonly IReceivableRepository _receRep;

        public DelReceivableCommandHandler(IReceivableRepository receRep)
        {
            _receRep = receRep;
        }

        public async Task<int> Handle(DelReceivableCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            foreach (var item in request.ids)
            {
                var res = (await _receRep.GetAllAsync()).FirstOrDefault(x => x.ReceivableId == item);
                if (res != null)
                {
                    res.IsDel = true;
                    res.DelName = request.DelName;
                    res.DelTime = request.DelTime;
                    await _receRep.Update(res);
                    count = 1;
                }
                else
                {
                    count = -1;
                }
            }
            return count;
        }
    }
}
